from django.shortcuts import render

# Create your views here.

# GET /areas/   获取列表数据
from rest_framework.generics import ListAPIView, RetrieveAPIView


# class AreasView(ListAPIView):
#     pass


# 获取子级数据
# 需要有上一级的area_id，作为pk传递过来
# 获取某一个省份，或者市的详情信息
# class SubAreasView(RetrieveAPIView):
#     pass

# 使用视图集可以将一系列逻辑相关动作放到一个类中
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from areas.models import Area
from .serializers import AreaSerializer,SubAreaSerializer


class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):

    # 禁止分页
    pagination_class = None

    # queryset = Area.objects.fitler(parent=None)  # 查询父集为空的信息
    # serializer_class =
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer